package com.zq.zhufengfm.utils;

/**
 * Project_name : ZhuFengFM
 * Author : zhaoQiang
 * Email : 229457269@qq.com
 * Create_time : 2015/10/20 | 17:06
 */

import com.zq.zhufengfm.Constants;
import com.zq.zhufengfm.entity.DiscoverCategroy;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendAlubums;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendItem;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendSpecial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * 实体类的解析工具类：
 */
public final class EntityParseUtil {

    public EntityParseUtil() {
    }

    public static List<DiscoverCategroy> parseDiscoveryCategory(JSONObject json){

        List<DiscoverCategroy> ret  = null;

        if (json != null) {
            try {
                int code = json.getInt("ret");

                if (code == Constants.TASK_RESULT_OK) {
                    ret = new LinkedList<>();

                    JSONArray array = json.getJSONArray("list");
                    int len = array.length();
                    if (len > 0) {
                        for (int i = 0; i < len; i++) {

                            JSONObject jsonObject = array.getJSONObject(i);

                            DiscoverCategroy category = new DiscoverCategroy();

                            category.parseJSON(jsonObject);

                            ret.add(category);
                        }
                    }
                }else{

                }
            } catch (Exception e) {

            }

        }
        return ret;
    }


    /**
     * 解析 "发现模块中"  推荐  栏目中   的数据结构：
     * @param jsonObject
     * @return
     */
    public static List<DiscoverRecommendItem> parseDiscoverCommened(JSONObject jsonObject) {
        List<DiscoverRecommendItem> ret = null;

        if (jsonObject != null) {

            try {
                int code = jsonObject.getInt("ret");

                if (code == Constants.TASK_RESULT_OK) {

                    ret = new LinkedList<DiscoverRecommendItem>();

                    //------------------------解析广告标题   轮播广告  未在此解析

                    //----------------------------
                    //TODO 小编推荐内容的解析：
                    JSONObject object = jsonObject.getJSONObject(
                            "editorRecommendAlbums");

                    DiscoverRecommendAlubums  editorRecommend
                             = new DiscoverRecommendAlubums();

                    editorRecommend.parseJSON(object);

                    ret.add(editorRecommend);
                    //--------------------------------------
                    //TODO  解析精品听单

                    JSONObject specialColumnJSON = jsonObject.getJSONObject("specialColumn");

                    DiscoverRecommendSpecial special = new DiscoverRecommendSpecial();

                    special.parseJSON(specialColumnJSON);

                    ret.add(special);

                    //------------------------------
                    //TODO 解析发现新奇



                    //-------------------------------
                    //TODO 解析热门推荐内部的内容：
                    JSONObject hotRecommendJson = jsonObject.
                            getJSONObject("hotRecommends");

                    JSONArray hotList = hotRecommendJson.getJSONArray("list");

                    for (int i = 0; i < hotList.length() ; i++) {

                        //获取热门推荐  list中每一个推荐信息
                        JSONObject discoverRecommendAlbumJson =
                                hotList.getJSONObject(i);

                        DiscoverRecommendAlubums albums = new
                                DiscoverRecommendAlubums();

                        albums.parseJSON(discoverRecommendAlbumJson);

                        ret.add(albums);

                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}
